package com.googlecode.grt192.HH11.sensor;

import java.util.Vector;

import com.googlecode.grt192.HH11.event.SafetyEvent;
import com.googlecode.grt192.HH11.event.SafetyListener;
import com.googlecode.grtframework.event.SwitchEvent;
import com.googlecode.grtframework.event.SwitchListener;
import com.googlecode.grtframework.sensor.ISwitch;

/**
 * Haunted House Safety sends Safety events to Hardware.
 * 
 * It reads from a button press switch.
 * 
 * @author ajc, agd
 * 
 */
public class HHSafety implements SwitchListener {

	private Vector<SafetyListener> listeners;
	private ISwitch sw;

	public HHSafety(ISwitch sw) {
		this.sw = sw;
		listeners = new Vector<SafetyListener>();
	}

	public void addSafetyListener(SafetyListener listener) {
		listeners.add(listener);
	}

	public void removeSafetyListener(SafetyListener listener) {
		listeners.remove(listener);
	}

	@Override
	public void switchPressed(SwitchEvent arg0) {

	}

	@Override
	public void switchReleased(SwitchEvent arg0) {
		SafetyEvent safetyEvent = new SafetyEvent(this, 1); // TODO magic number
		// what is 1
		for (int i = 0; i < listeners.size(); i++) {
			listeners.get(i).suspend(safetyEvent);
		}

	}

	/*
	 * Start listening to the switch
	 */
	@Override
	public void startListening() {
		sw.addSwitchListener(this);

	}

	/*
	 * Stop listening to the switch
	 */
	@Override
	public void stopListening() {
		sw.removeSwitchListener(this);

	}

}
